package kr.co.fashionmai.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.co.fashionmai.service.FabricService;
import kr.co.fashionmai.util.FabricPageUtil;
import kr.co.fashionmai.vo.FabricHistory;
import kr.co.fashionmai.vo.FabricMain;
import kr.co.fashionmai.vo.FabricSub;

import net.sf.json.JSONObject;
import net.sf.json.util.JSONBuilder;

import org.apache.jasper.tagplugins.jstl.core.Redirect;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class FabricController 
{
	private FabricService fabricService;
	
	public void setFabricService(FabricService fabricService)
	{
		this.fabricService = fabricService;
	}
	
	@RequestMapping(value="fabricMain.do")
	public ModelAndView fabricMain(FabricMain fabricMain)
	{	
		ModelAndView modelAndView = new ModelAndView("fabricMain");
		int totalCount = fabricService.getTotalCount(fabricMain);
		
		int pageNo = fabricMain.getPageNo();
		if(pageNo==0) pageNo=1;
		
		FabricPageUtil fabricPageUtil = new FabricPageUtil(pageNo, totalCount);
		modelAndView.addObject("pasing", fabricPageUtil.getPageStr());
		
		int queryPageNo = (pageNo-1)*20;
		fabricMain.setPageNo(queryPageNo);
 		
 		List<FabricMain> list = fabricService.main(fabricMain);
 		fabricMain.setPageNo(pageNo);
		modelAndView.addObject("fabricList", list);
		return modelAndView;
	}
	
	@RequestMapping(value="fabricInsert.do")
	public String insert(FabricMain fabricMain, HttpServletRequest request)
	{
		String[] color = request.getParameterValues("color");
		String[] saving = request.getParameterValues("saving");
		String[] yards = request.getParameterValues("yards");
		String etc = request.getParameter("etc");
		String updater = request.getParameter("updater");
		
		List<FabricSub> fabricSubList = new ArrayList<FabricSub>();
		int paramSize = color.length;
		for(int i=0; i<paramSize; i++)
		{
			String tempColor = color[i];
			int tempSaving = Integer.parseInt(saving[i]);
			int tempYards = Integer.parseInt(yards[i]);
			FabricSub fabricSub = new FabricSub(fabricMain.getFabric(), tempColor, tempSaving, tempYards);
			fabricSubList.add(fabricSub);
		}
		
		fabricService.insert(fabricMain);
		fabricService.insert(fabricSubList);
		fabricService.insert(new FabricHistory(fabricMain.getFabric(), fabricMain.getStockCount(), updater));
		
		
		return "redirect:fabricMain.do";
	}
	
	@RequestMapping(value="fabricDelete.do")
	public String delete(@RequestParam(value="fabric") String fabric)
	{
		fabricService.delete(fabric);
		return "redirect:fabricMain.do";
	}
	
	@RequestMapping(value="fabricSelect.do")
	public void select(@RequestParam(value="fabric") String fabric, HttpServletResponse response)
	{
		response.setCharacterEncoding("UTF-8");
		
		HashMap map = fabricService.select(fabric);
		String etc = (String) map.get("etc");
		List subList = (List) map.get("subList");
		List historyList = (List) map.get("historyList");
		
		JSONObject jsonObject = JSONObject.fromObject(map);
		
		PrintWriter writer = null;
		
		try 
		{
			writer = response.getWriter();
			writer.write(jsonObject.toString());
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			writer.close();
		}
	}
	
	@RequestMapping(value="fabricUpdate.do")
	public String update(FabricMain fabricMain, HttpServletRequest request)
	{
		fabricService.delete(fabricMain.getFabric(),"update");
		
		String[] color = request.getParameterValues("color");
		String[] saving = request.getParameterValues("saving");
		String[] yards = request.getParameterValues("yards");
		String etc = request.getParameter("etc");
		String updater = request.getParameter("updater");
		
		List<FabricSub> fabricSubList = new ArrayList<FabricSub>();
		int paramSize = color.length;
		
		for(int i=0; i<paramSize; i++)
		{
			String tempColor = color[i];
			int tempSaving = Integer.parseInt(saving[i]);
			int tempYards = Integer.parseInt(yards[i]);
			FabricSub fabricSub = new FabricSub(fabricMain.getFabric(), tempColor, tempSaving, tempYards);
			fabricSubList.add(fabricSub);
		}
		
		fabricService.insert(fabricMain);
		fabricService.insert(fabricSubList);
		fabricService.insert(new FabricHistory(fabricMain.getFabric(), fabricMain.getStockCount(), updater));
		fabricService.update(fabricMain.getFabric());
		return "redirect:fabricMain.do";
	}
}

















